/* -----------------------------------------------------------------------------
 * PROJECT    : lwf
 * FILE       : AlgebraAction.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Feb 16, 2011  mycodetalks			       Created
 */
package net.lwf.web.struts.action.maths;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lwf.handler.IProcessHandler;
import lwf.web.struts.action.constant.IActionConstant;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.math.algebra.AlgebraProcessHandler;
import net.lwf.util.web.WebUtil;
import net.lwf.web.constant.IPageTitle;
import net.lwf.web.constant.IWebConstant;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


/**
 * Class description goes here
 */
public class AlgebraAction extends ArithmaticAction {
	
	private static final String prefix = "Algebra";
	
	public ActionForward home(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String operation = getOperation(request);
		WebUtil.removeAttribute(request, IWebConstant.DECIMALS);
		setRequestData(request);
		ArithmaticActionForm aaform = (ArithmaticActionForm) form;
		aaform.setAskQuestion(false);
		//resetOperation(request);
		aaform.setTitle(getTitle(operation, prefix)); //TBR
		//setBasicFormInfo(request, getTitle(operation, prefix), getQuizLink(), getPdfLink());
		aaform.setOperation(operation.toLowerCase());

		return mapping.findForward("home");
	} //home
	
	public ActionForward generate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		//String operation = getOperation(request);
		String operation = getOperation();
		//Get Handler
		AlgebraProcessHandler handler = (AlgebraProcessHandler) getHandler(request);
		QuestionInfo aq = handler.generateQuestion();
		//Set the question data
		setQuestionData(request, aq);
		//Set form data
		ArithmaticActionForm aaform = (ArithmaticActionForm) form;
		aaform.setOperation(operation.toLowerCase());
		aaform.setHtmlQuestion(aq.getHtmlQuestion());
		aaform.setAskQuestion(true);
		aaform.setTitle(getTitle(operation, prefix)); //TBR
		//setBasicFormInfo(request, getTitle(operation, prefix), getQuizLink(), getPdfLink());
		aaform.setQuestionNumber(getQuestionNumber(request));
		
		return mapping.findForward("success");
	} //generate
	
	@Override
	public IProcessHandler getHandler(HttpServletRequest request) {
		//Think whether to keep handler in session or not - TODO
		//return new AlgebraProcessHandler(getGrade(), getOperation(request));
		String decimals = (String) WebUtil.getAttribute(request, IWebConstant.DECIMALS);
		if(decimals != null) {
			return new AlgebraProcessHandler(getGrade(), getOperation(), true);
		}
		return new AlgebraProcessHandler(getGrade(), getOperation());
	}
	
	@Override
	public String getPdfLink() {
		return IActionConstant.ALGEBRA_PDF_LINK;
	}

	@Override
	public String getQuizLink() {
		return IActionConstant.ALGEBRA_QUIZ_LINK;
	}
	
	@Override
	public String getMainTitle() {
		return IPageTitle.ALGEBRA_MAIN;
	}

}



